package com.example.mybatisplus.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.model.domain.BuildingManagement;
import com.example.mybatisplus.model.dto.DeleteDTO;
import com.example.mybatisplus.model.dto.PageDTO;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.BuildingManagementService;
import com.example.mybatisplus.model.domain.BuildingManagement;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/api/buildingManagement")
public class BuildingManagementController {

    @Autowired
    private BuildingManagementService buildingManagementService;

    @RequestMapping(value = "/importbuilding")
    public JsonResponse upload(MultipartFile file) throws IOException{
        buildingManagementService.importbuilding(file);
        return JsonResponse.success(true);
    }

    @GetMapping("removeById")
    public JsonResponse removeById(Long id){
        boolean b = buildingManagementService.removeById(id);
        return JsonResponse.success(b);
    }

    @PostMapping("edit")
    public JsonResponse edit(@RequestBody BuildingManagement buildingManagement){
        boolean b = buildingManagementService.updateById(buildingManagement);
        return JsonResponse.success(b);
    }

    @GetMapping("pageList")
    public JsonResponse pageList(BuildingManagement buildingManagement, PageDTO dto){
        Page<BuildingManagement> page = buildingManagementService.pageList(buildingManagement, dto);
        return JsonResponse.success(page);
    }

    @PostMapping("removeById2")
    public JsonResponse removeById2(@RequestBody BuildingManagement buildingManagement){
        boolean b = buildingManagementService.removeById(buildingManagement.getBuildingId());
        return JsonResponse.success(b);
    }

    @PostMapping("removeByIds")
    public JsonResponse removeByIds(@RequestBody DeleteDTO dto){
        boolean b = buildingManagementService.removeByIds(dto.getIds());
        return JsonResponse.success(b);
    }

    @PostMapping("addById")
    public JsonResponse addById(@RequestBody BuildingManagement buildingManagement){
        boolean b = buildingManagementService.saveOrUpdate(buildingManagement);
        return JsonResponse.success(b);
    }

    @GetMapping("/updateTotalRooms/{buildingId}")
    public void updateTotalRoomsForBuilding(@PathVariable String buildingId) {
        buildingManagementService.updateTotalRoomsForBuilding(buildingId);
    }
    @GetMapping("/totalRooms/{buildingId}")
    public ResponseEntity<?> getTotalRooms(@PathVariable String buildingId) {
        try {
            int totalRooms = buildingManagementService.getTotalRoomsForBuilding(buildingId);
            return ResponseEntity.ok().body(totalRooms);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("计算房间总数失败");
        }
    }
}
